import { BaseModel } from '@/common/base/base.model';
import {
  Column,
  Entity,
  ManyToOne,
  JoinColumn,
  OneToOne,
} from 'typeorm';
import { UserModel } from '@/module/user/user.model';
import { ArticleExtModel } from '@/module/article-ext/article-ext.model';
import { ArticleNormalCategory, ArticleStatus, ArticleType, BooleanNumber } from 'common/utils/data';
import { EvaluationModel } from "@/module/evaluation/evaluation.model";
import { numberTransformer } from "@/common/utils/utils";

@Entity('ARTICLE', {
  synchronize: false,
}) // 文章
export class ArticleModel extends BaseModel {
  @Column({
    comment:
      '状态（DRAFT：草稿, REVIEW:审核中, PUBLISH: 已发布, REJECT：被驳回 ）',
    type: 'varchar',
    length: 50,
    default: ArticleStatus.DRAFT,
  })
  status: ArticleStatus;

  @Column({
    comment: '分类，支持多个',
    type: 'simple-array',
    default: [],
  })
  categories: ArticleNormalCategory[];

  @Column({
    comment: '标题',
    type: 'varchar',
    length: 50,
  })
  title: string;

  @Column({
    comment: '用户uuid',
    type: 'uuid',
  })
  userUuid: string;
  @ManyToOne(() => UserModel, {
    createForeignKeyConstraints: false
  })
  @JoinColumn({
    name: 'user_uuid',
    referencedColumnName: 'uuid',
  })
  user: UserModel;

  @Column({
    comment: '状态（1：置顶，0：不置顶）',
    type: 'smallint',
    default: BooleanNumber.false,
  })
  isStick: BooleanNumber;

  @Column({
    comment: '缩略图url',
    type: 'simple-array',
    default: [],
  })
  thumbnails: string[];

  @Column({
    comment: '通过文章内容提炼的缩略图url',
    type: 'simple-array',
    default: [],
  })
  thumbnailsByContent: string[];

  @Column({
    comment: '关键词',
    type: 'simple-array',
    default: '',
  })
  keywords: string[];

  @Column({
    comment: '描述',
    type: 'varchar',
    length: 1000,
    default: '',
  })
  desc: string;

  @Column({
    comment: '通过文章内容提炼的描述',
    type: 'varchar',
    length: 1000,
    default: '',
  })
  descByContent: string;

  @Column({
    comment: '最近发表时间',
    type: 'bigint',
    nullable: true,
    transformer: numberTransformer,
  })
  publishTime: number;

  @Column({
    comment: '最近评论时间（不包含发文章人自己）',
    type: 'bigint',
    nullable: true,
    transformer: numberTransformer,
  })
  commentTime: number;

  @Column({
    comment: '评论数',
    type: 'int',
    default: 0,
  })
  commentCount: number;

  @Column({
    comment: '类型（NORMAL：普通富文本, EVALUATION:评测）',
    type: 'varchar',
    length: 50,
    default: ArticleType.NORMAL,
  })
  type: ArticleType;

  /** 社区文章 */
  @OneToOne(() => ArticleExtModel, (ext) => ext.article)
  ext: ArticleExtModel;

  /** 评测 */
  @OneToOne(() => EvaluationModel, (evaluation) => evaluation.article)
  evaluation: EvaluationModel;
}
